using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Wrath.Xml.Objects
{
    public enum WindowClassToolTipType
    {
        Text,
        Field,
        Control
    }

    public class WindowClassTooltip : IElementContainer
    {
        private WindowClassToolTipType toolTipType;
        private string toolTip;

        public WindowClassToolTipType Type
        {
            get { return toolTipType; }
            set { toolTipType = value; }
        }

        public string Text
        {
            get { return toolTip; }
            set { toolTip = value; }
        }

        public WindowClassTooltip(Element element)
        {
            sourceElement = element;

            Element subElement = sourceElement.FindFirstElement("text", false);
            if (subElement != null)
            {
                toolTip = subElement.Value.Substring(0);
                toolTipType = WindowClassToolTipType.Text;
            }
            else
            {
                subElement = sourceElement.FindFirstElement("field", false);
                if (subElement != null)
                {
                    toolTip = subElement.Value.Substring(0);
                    toolTipType = WindowClassToolTipType.Field;
                }
                else
                {
                    subElement = sourceElement.FindFirstElement("control", false);
                    if (subElement != null)
                    {
                        toolTip = subElement.Value.Substring(0);
                        toolTipType = WindowClassToolTipType.Control;
                    }
                    else
                    {
                        throw new InvalidDataException("Tooltip element contains no valid child elements.");
                    }
                }
            }
        }

        #region IElementContainer Members

        private Element sourceElement;

        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("tooltip");

            Element subElement = new Element();

            switch (toolTipType)
            {
                case WindowClassToolTipType.Text: subElement.Name = "text"; break;
                case WindowClassToolTipType.Field: subElement.Name = "field"; break;
                case WindowClassToolTipType.Control: subElement.Name = "control"; break;
            }

            subElement.Value = toolTip;

            sourceElement.ChildElements.Add(subElement);
        }

        #endregion
    }
}
